package com.starblink.login.createaccount.register.ui

import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.basic.route.RoutePage
import com.starblink.android.basic.base.activity.BaseTVMActivity
import android.os.Bundle
import android.view.View
import com.starblink.android.basic.user.login.LoginManager
import com.starblink.login.databinding.ActivityCreateAccountRegisterBinding

/**
 * 创建账户-注册
 */
@Route(path = RoutePage.Login.CREATE_ACCOUNT_REGISTER)
class CreateAccountRegisterActivity :
    BaseTVMActivity<ActivityCreateAccountRegisterBinding, CreateAccountRegisterVM>() {

    override fun initViewBinding(): ActivityCreateAccountRegisterBinding =
        ActivityCreateAccountRegisterBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewBinding.etUsername.setOnEditInputListener {
            viewBinding.tvErrorHint.visibility = View.GONE
            viewBinding.btnRegister.isEnabled = !it.isEmpty()
        }

        viewBinding.btnRegister.isEnabled = LoginManager.validateUserName(viewBinding.etUsername.text) == null
        viewBinding.btnRegister.setOnClickListener {
            val res = LoginManager.validateUserName(viewBinding.etUsername.text)
            if (res == null) { // 输入用户名OK
                viewModel.register(
                    intent.getStringExtra(EXTRA_EMAIL)!!,
                    intent.getStringExtra(EXTRA_PSW)!!,
                    viewBinding.etUsername.text.trim())
            } else {
                viewBinding.tvErrorHint.visibility = View.VISIBLE
                viewBinding.tvErrorHint.text = res
            }
        }
    }
    companion object {
        const val EXTRA_EMAIL = "EXTRA_EMAIL"
        const val EXTRA_PSW = "EXTRA_PSW"
    }
}